var express = require('express');
var router = express.Router();
// const schedulesService = require('../src/service/schedules/schedulesService.js')
const schedulesService = require('../src/services/schedules/schedulesService.js')

//引入上传文件的方法
const {
  uploadFile
} = require("../src/utils/upload");

/* GET home page. */
// 显示所有信息
router.get('/showsAll', async function (req, res, next) {
  let result = await schedulesService.showAllInfo();
  console.log(result);
  res.json({
    "status": 0,
    "message": "",
    "total": 180,
    "datas": result
  })
});
// 添加 排片信息
router.post('/addSchedules', async function (req, res, next) {
  let {
    movieId,
    cinemasId,
    theaterId,
    showTime,
    price,
    photo
  } = req.body;
  console.log("showTime", showTime);
  let result = await schedulesService.insertSchedulesInfo(movieId, cinemasId, theaterId, showTime, price, photo);
  console.log(result);
  if (result) {
    res.json({
      "code": 1,
      "msg": "添加成功"
    })
  } else {
    res.json({
      "code": 0,
      "msg": "添加失败"
    })
  }
});

//删除 排片信息
router.delete('/delSchedules/:id', async function (req, res, next) {
  let {
    id
  } = req.params;
  let result = await schedulesService.deleteSchedulesInfo(id);
  console.log(result);
  if (result.deletedCount == 1) {
    res.json({
      "code": 1,
      "msg": "删除成功"
    })
  } else {
    res.json({
      "code": 0,
      "msg": "删除失败"
    })
  }
});

//查看 排片信息
router.get('/showSchedules/:id', async function (req, res, next) {
  let {
    id
  } = req.params;
  let condition = {}
  condition._id = id
  console.log(id);
  let result = await schedulesService.showSchedulesInfo(condition);
  console.log("1111111", result[0]);
  if (result) {
    res.json({
      "code": 1,
      "msg": "查询成功",
      "data": result[0]
    })
  } else {
    res.json({
      "code": 0,
      "msg": "查询失败"
    })
  }
});


// 条件查询
router.get('/condition', async function (req, res, next) {
  let {
    movieId,
    showTime
  } = req.query;
  // 创建一个空对象
  let condition = {};
  // 电影名
  if (movieId != null) {
    condition.movieId = movieId;
  };
  // 时间
  if (showTime != null) {
    condition.showTime = showTime;
  };
  console.log(condition);
  let result = await schedulesService.showSchedulesInfo(condition);
  console.log("1111111", result);
  if (result) {
    res.json({
      "status": 0,
      "message": "",
      "total": 180,
      "datas": result
    })
  } else {
    res.json({
      "code": 1,
      "msg": "查询失败"
    })
  };
});


//修改 排片信息
router.put('/updatesSchedules/', async function (req, res, next) {
  let {
    _id,
    movieId,
    cinemasId,
    theaterId,
    showTime,
    price
  } = req.body;
  let result = await schedulesService.updateSchedulesInfo({
    _id,
    movieId,
    cinemasId,
    theaterId,
    showTime,
    price
  });
  // console.log(result);
  if (result.modifiedCount == 1) {
    res.json({
      "code": 1,
      "msg": "修改成功",
      "data": result
    })
  } else {
    res.json({
      "code": 0,
      "msg": "修改失败"
    })
  };
});


//分页
router.get('/page', async function (req, resp) {
  let {
    currentpage,
    pagesize
  } = req.query;

  console.log(currentpage, pagesize);

  let pageBean = await schedulesService.getAccessToPag({
    "currentpage": parseInt(currentpage),
    "pagesize": parseInt(pagesize)
  })

  console.log("pageBean=========",pageBean);
  resp.json({
    "status": 0,
    "message": "success",
    ...pageBean,
  })
});





module.exports = router;